package orm

import (
	"sync"
)

var (
	builderDriverOnce sync.Once
	builderDriver *Driver
)

// Driver (原 BuilderDriver )数据库驱动模块, 被 Engine 和 Builder 依赖, 根据驱动来搞事情。
type Driver struct {
	builders map[string]IBuilder
	b        *sync.Map
}

// NewDriver (原 NewBuilderDriver)...
func NewDriver() *Driver {
	builderDriverOnce.Do(func() {
		//builderDriver = &BuilderDriver{builders:make(map[string]IBuilder)}
		builderDriver = &Driver{b: &sync.Map{}}
	})
	return builderDriver
}

// Register 注册一个驱动。
func (b *Driver) Register(driver string, val IBuilder) {
	b.b.Store(driver, val)
}

// Get 获取一个驱动。
func (b *Driver) Get(driver string) IBuilder {
	if v, ok := b.b.Load(driver); ok {
		return v.(IBuilder)
	}
	return nil
}
